<?php namespace Phpcmf\Model\Fuwu;

// 自动执行方法
class Op extends \Phpcmf\Model
{

    public function is_favorite($tablename, $dirname) {

        if (!dr_in_array('favorites', \Phpcmf\Service::M('table')->get_cache_field($tablename)) ) {
            return dr_return_data(0, dr_lang('应用[模块内容收藏123]未安装到本模块[%s]', $dirname));
        }

        $id = (int)\Phpcmf\Service::L('input')->get('id');

        if (!$this->uid) {
            return dr_return_data(0, dr_lang('还没有登录'));
        } elseif (!$id) {
            return dr_return_data(0, dr_lang('id参数不完整'));
        }

        $favorite = $this->db->table($tablename.'_favorite')->where('cid', $id)->where('uid', $this->uid)->countAllResults();
        if ($favorite) {
            return dr_return_data(1, dr_lang('已经收藏'));
        } else {
            return dr_return_data(0, dr_lang('没有收藏'));
        }
    }
    
    public function run($tablename, $dirname) {

        /*if (!dr_in_array('favorites', \Phpcmf\Service::M('table')->get_cache_field($tablename)) ) {
            return dr_return_data(0, dr_lang('应用[模块内容收藏123]未安装到本模块[%s]', $dirname));
        }*/

        $id = (int)\Phpcmf\Service::L('input')->get('id');
        if (!$this->uid) {
            return dr_return_data(0, dr_lang('还没有登录'));
        } elseif (!$id) {
            return dr_return_data(0, dr_lang('id参数不完整'));
        }

        /*$data = $this->db->table($tablename.'_index')->where('id', $id)->countAllResults();
        if (!$data) {
            return dr_return_data(0, dr_lang('模块内容不存在'.$tablename));
        }*/

        $table = $tablename.'_form_son_favorite';
        if (!$this->db->tableExists($table) ) {
            return dr_return_data(0, dr_lang('应用[模块内容收藏]未安装到本模块[%s]', $dirname));
        }

        $favorite = $this->db->table($table)->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
        if ($favorite) {
            // 已经收藏了,我们就删除它
            $msg = dr_lang('取消收藏');
            $this->db->table($table)->where('id', intval($favorite['id']))->delete();
        } else {
            if (!\Phpcmf\Service::M()->db->fieldExists('inputtime', $table)) {
                \Phpcmf\Service::M()->query('ALTER TABLE `'.$table.'` ADD `inputtime` int(10) unsigned DEFAULT \'0\' COMMENT \'操作时间\';');
            }
            $msg = dr_lang('收藏成功');
			
            $this->db->table($table)->insert(array(
                'cid' => $id,
                'uid' => $this->uid,
                'inputtime' => SYS_TIME
            ));
        }

        // 更新数量
        $c = $this->db->table($tablename.'_form_son_favorite')->where('cid', $id)->countAllResults();
        $this->db->table($tablename.'_form_son')->where('id', $id)->set('favorites', $c)->update();
        \Phpcmf\Service::M('cache')->update_data_cache();

        // 返回结果
        return dr_return_data(1, $msg, $c, $c);

    }
	/*点赞*/
	public function zan($tablename, $dirname) {
	
	    /*if (!dr_in_array('favorites', \Phpcmf\Service::M('table')->get_cache_field($tablename)) ) {
	        return dr_return_data(0, dr_lang('应用[模块内容收藏123]未安装到本模块[%s]', $dirname));
	    }*/
	
	    $id = (int)\Phpcmf\Service::L('input')->get('id');
	    if (!$this->uid) {
	        return dr_return_data(0, dr_lang('还没有登录'));
	    } elseif (!$id) {
	        return dr_return_data(0, dr_lang('id参数不完整'));
	    }
	
	    /*$data = $this->db->table($tablename.'_index')->where('id', $id)->countAllResults();
	    if (!$data) {
	        return dr_return_data(0, dr_lang('模块内容不存在'.$tablename));
	    }*/
	
	    $table = $tablename.'_form_son_zan';
	    if (!$this->db->tableExists($table) ) {
	        return dr_return_data(0, dr_lang('应用[模块内容点赞]未安装到本模块[%s]', $dirname));
	    }
	
	    $zan = $this->db->table($table)->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
	    if ($zan) {
	        // 已经点赞了,我们就删除它
			//取消点赞奖励积分
			$url='/index.php?s=quan&c=son&m=index&id='.$id;
			\Phpcmf\Service::M('member')->add_score($this->uid, -1, $note = '取消点赞扣取', $url, $mark = '', $count = 1);
	        $msg = dr_lang('取消点赞 -1 积分');
	        $this->db->table($table)->where('id', intval($zan['id']))->delete();
	    } else {
	        if (!\Phpcmf\Service::M()->db->fieldExists('inputtime', $table)) {
	            \Phpcmf\Service::M()->query('ALTER TABLE `'.$table.'` ADD `inputtime` int(10) unsigned DEFAULT \'0\' COMMENT \'操作时间\';');
	        }
			//点赞奖励积分
			$url='/index.php?s=quan&c=son&m=index&id='.$id;
			\Phpcmf\Service::M('member')->add_score($this->uid, 1, $note = '点赞奖励', $url, $mark = '', $count = 1);
	        $msg = dr_lang('点赞成功 +1 积分');
			
	        $this->db->table($table)->insert(array(
	            'cid' => $id,
	            'uid' => $this->uid,
	            'inputtime' => SYS_TIME
	        ));
	    }
	
	    // 更新数量
	    $c = $this->db->table($tablename.'_form_son_zan')->where('cid', $id)->countAllResults();
	    $this->db->table($tablename.'_form_son')->where('id', $id)->set('zan', $c)->update();
	    \Phpcmf\Service::M('cache')->update_data_cache();
	
	    // 返回结果
	    return dr_return_data(1, $msg, $c, $c);
	
	}


}